package ntd.util;

import cn.hutool.core.util.ObjectUtil;
import lombok.SneakyThrows;
import ntd.util.fun.ComFun;

import java.util.HashSet;
import java.util.Set;

public class UtilRun {

    private static Set<String> codes = new HashSet<>();

    @SneakyThrows
    public static void onlyOne(String code, ComFun.Fun run) {
        if (codes.contains(code)) {
            return;
        }
        codes.add(code);
        run.accept();
    }

    @SneakyThrows
    public static void jitterRun(String code, ComFun.Fun run, long time) {
        if (ToolCache.lock(code, time)) {
            run.accept();
        }
    }

    @SneakyThrows
    public static void notNullRun(Object obj, ComFun.Fun run) {
        if (ObjectUtil.isAllNotEmpty(obj, run)) {
            run.accept();
        }
    }
}
